import { defineStore } from "pinia";
import { whitePagePost, detailListGet, deteleListDel } from "@/api/blackAndWhite"
import { ElMessage } from "element-plus";
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';

export const whiteListStore = defineStore("whiteListStoreId", () => {
const { global } = useInstance();

    const detailData = ref([

        {
            type: "text",
            prop: "value",
            name: "白名单值",
            value: null
        },
        {
            type: "text",
            prop: "reason",
            name: "进入白名单原因",
            value: null
        },
        {
            type: "text",
            prop: "type",
            name: "白名单类型",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "whiteList",
            propsList: [
                {
                    label: '白名单值',
                    prop: 'value',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '进入白名单原因',
                    prop: 'reason',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '白名单类型',
                    prop: 'type',
                    type: 'custom',
                    slotName: 'type',
                    minWidth: '200px',
                    hidden: true,
                },

                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '150px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()

    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            type: null,//白名单类型 "TCL" 
            value: null,//白名单值 "TCL" 
            reason: null,//进入白名单原因 "TCL" 

        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],
    })
    const addAndEditRef = ref<{ show: (type: string, row?: any) => void }>()
    const getDate1 = async (type: string) => {
        if(type == 'reset'){
            listSearch.condition.reason = null
            listSearch.condition.value = null
            listSearch.condition.type = null
            listSearch.pageNum = 1
            listSearch.pageSize = 20
        }
        try {
            pageParams.loadIng = true
            const res = await whitePagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records.map((item: any) => {
                    return {
                        ...item,
                        type: item.type + ''
                    }
                })
                pageParams.loadIng = false
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);

        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const AddEdit = async (type: string, row?: any) => {
        if (type == 'add') {
            addAndEditRef.value?.show(type)
        } else {
            try {
                const res = await detailListGet({ id: row.id })
                res.data.type = res.data.type + ''
                addAndEditRef.value?.show(type, res.data)
            } catch (error) {
                console.log(error);

            }

        }
    }

    const delBtn =async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.value}`)
        if (flag) {
            const res = await deteleListDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const handPage = (params: any) => {
        console.log(params);
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    return {
        detailData, tableProp, listSearch, pageParams, addAndEditRef, detailRef,
        getDate1, AddEdit, delBtn, handPage
    }
})